      SUBROUTINE PK_NOMISS(KFILDO,IA,IB,NXY,MINPK,IFIRST,
     1                     ISECOND,IMINA,NUMOCTET,SECOND,
     2                     IER)
C
C        FEBRUARY 1994   GLAHN   TDL   MOS-2000
C        MARCH    1997   GLAHN   SLIGHT DIAGNOSTIC CHANGE.
C        MARCH    2000   GLAHN   REVISED FOR GRIB2;
C                                CHANGED NAME FROM PACKXX
C        APRIL    2000   LAWRENCE REVISED FOR STILL MORE UPDATES
C                                TO GRIB2 DOCUMENTATION.
C        JANUARY  2001   GLAHN   COMMENTS; ELIMINATED ARRAY IBXX2( );
C                                CHANGED IER = 1100 TO 920
C        NOVEMBER 2001   GLAHN   ADDED IER=0 AT BEGINNING;
C                                ELIMINATED UNUSED IZER0
C        NOVEMBER 2001   GLAHN   CHANGED NUMOCTET=(NUMBIT/8)+1 TO
C                                NUMOCTET=(NUMBIT+7)/8 NEAR THE END
C        JANUARY  2002   GLAHN   ADDED TEST FOR 1ST ORDER DIFF GE
C                                2**15.; MODIFIED COMMENT FOR IER = 920
C        JANUARY  2002   GLAHN   ADDED MALLOW = 2**30+1 AND USED INSTEAD
C                                OF 999999 FOR JMAX AND JMIN
C        DECEMBER 2002   TAYLOR/GLAHN   CHANGED STATEMENT 225 FROM
C                                'IF(IVALUE.LT.0)NUMBIT=NUMBIT+1' TO
C                                NUMBIT=NUMBIT+1 TO ALLOW FOR SIGN
C
C        PURPOSE 
C            DETERMINES WHETHER OR NOT TO USE SECOND ORDER
C            DIFFERENCES OR ORIGINAL VALUES TO PACK.  ORIGINAL VALUES
C            ARE INDICATED WHEN THE AVERAGE RANGE OF CONSECUTIVE GROUPS
C            OF SIZE MINPK OF THE SECOND ORDER DIFFERENCES IS
C            LARGER THAN THE AVERAGE RANGE OF CONSECUTIVE GROUPS OF
C            SIZE MINPK OF THE ORIGINAL VALUES.  NOTE THAT THIS 
C            PROCEDURE DOES NOT IN GENERAL USE THE SAME GROUPS AS ARE
C            USED IN THE ACTUAL PACKING BECAUSE IT DOES NOT EMPLOY THE
C            LOOKBACK PROCEDURE.  THIS ALGORITHM IS RELATIVELY CHEAP
C            AND GIVES A RESULT GOOD ENOUGH FOR THE PURPOSE.  THIS
C            ROUTINE IS NOT USED WHEN THERE ARE MISSING VALUES 
C            (I.E., A MISSING VALUE INDICATOR IS IN THE DATA).
C            WHEN USING SECOND ORDER DIFFERENCES IS NOT TO BE DONE,
C            IA( ) IS UNCHANGED; WHEN THEY ARE TO BE USED, IA( )
C            CONTAINS THOSE DIFFERENCES WITH THE FIRST 2 LOCATIONS
C            DUMMY (ACTUALLY, IA(1) = IA(2) = 0.
C
C        DATA SET USE 
C           KFILDO - UNIT NUMBER FOR OUTPUT (PRINT) FILE. (OUTPUT) 
C
C        VARIABLES 
C              KFILDO = UNIT NUMBER FOR OUTPUT (PRINT) FILE.  (INPUT) 
C               IA(K) = HOLDS THE NXY ORIGINAL VALUES ON
C                       INPUT (K=1,NXY).
C                       HOLDS THE NXY-2 SECOND ORDER DIFFERENCES ON
C                       OUTPUT WHEN SECOND ORDER DIFFERENCES ARE TO BE
C                       USED.  IN THAT CASE, SECOND IS .TRUE.
C                       (INPUT-OUTPUT)
C               IB(K) = WORK ARRAY (K=1,NXY).  (INTERNAL)
C                 NXY = NUMBER OF VALUES IN IA( ) ON INPUT.
C                       USED AS DIMENSION OF IA( ) AND IB( ).
C                       (INPUT)
C               MINPK = INCREMENT IN WHICH RANGES WILL BE COMPUTED.
C                       (INPUT)
C              IFIRST = IFIRST IS THE FIRST ORIGINAL VALUE.  (OUTPUT)
C             ISECOND = ISECOND IS THE SECOND ORIGINAL VALUE. (OUTPUT)
C               IMINA = THE OVERALL MINIMUM VALUE OF THE SECOND ORDER
C                       SPATIAL DIFFERENCES. (OUTPUT)
C            NUMOCTET = THE MINIMUM NUMBER OF OCTETS REQUIRED TO
C                       PACK THE VALUES REPRESENTED BY IFIRST,
C                       ISECOND, AND IMINA. (OUTPUT)
C              SECOND = TRUE (FALSE) WHEN SECOND ORDER DIFFERENCES
C                       ARE (ARE NOT) TO BE USED.  (OUTPUT)
C                 IER = CONTAINS ANY ERROR CODES GENERATED BY THIS
C                       ROUTINE
C                         0 = GOOD RETURN
C                       920 = A VALUE LARGER THAN WHAT CAN BE PACKED
C                             INTO 30 BITS HAS BEEN ENCOUNTERED. 
C
C             LOCAL VARIABLES
C                AVGR = CONTAINS THE AVERAGE RANGE OF NXY VALUES
C                       IN INCREMENTS OF MINPK FOR THE ORIGINAL
C                       DATA FIELD IN IA( ).
C               AVGR2 = CONTAINS THE AVERAGE RANGE OF NXY VALUES
C                       IN INCREMENTS OF MINPK FOR THE DATA FIELD
C                       WITH SECOND ORDER DIFFERENCES IN IB( ).
C               CFEED = CONTAINS THE CHARACTER REPRESENTATION
C                       OF A PRINTER FORM FEED.
C               IFEED = CONTAINS THE INTEGER VALUE OF A PRINTER
C                       FORM FEED.
C              IRANGE = THE DIFFERENCE OF JMAX AND JMIN.
C              IVALUE = THE VALUE OF IFIRST, ISECOND, AND IMINA
C                       THAT WILL NEED THE MOST OCTETS TO STORE.
C           JMAX,JMIN = USED TO KEEP TRACK OF THE MAX AND MIN VALUES
C                       OF A PARTICULAR GROUP OF MINPK VALUES.
C              JFIRST = CONTAINS THE ABSOLUTE VALUE OF THE FIRST
C                       NON-MISSING VALUE IN THE ORIGINAL DATA
C                       FIELD.
C            JLARGEST = CONTAINS THE LARGEST OF JFIRST, JSECOND, AND
C                       JMINA FOR THE PURPOSE OF DETERMINING
C                       HOW MANY OCTETS TO STORE ALL THREE VALUES
C                       IN.
C               JMINA = CONTAINS THE ABSOLUTE VALUE OF THE MINIMUM
C                       OF THE FIELD OF SECOND ORDER DIFFERENCES.
C             JSECOND = CONTAINS THE ABSOLUTE VALUE OF THE SECOND
C                       NON-MISSING VALUE IN THE ORIGINAL DATA
C                       FIELD.
C                  K = LOOP INDEX VARIABLE.
C               KOUNT = A COUNTING/LOOPING VARIABLE.
C                SUMR = USED IN COMPUTING THE AVERAGE RANGE OF THE
C                       ORIGINAL NXY VALUES IN INCREMENTS OF MINPK.
C               LARGE = 2**15, THE LARGEST 1ST ORDER DIFFERENCE
C                       TOLERATED.  IF THE 1ST ORDER DIFFERENCE 
C                       EXCEEDS THIS VALUE, IT IS LIKELY THE 2ND
C                       ORDER DIFFERENCE WILL OVERFLOW THE INTEGER
C                       COMPUTATION AND CAUSE AN UNDETECTED ERROR.
C
C        NON SYSTEM SUBROUTINES CALLED
C           NONE
C
      PARAMETER (MALLOW=2**30+1)
      PARAMETER (LARGE=2**15)
C
      CHARACTER*1 CFEED
      LOGICAL SECOND
C
      DIMENSION IA(NXY),IB(NXY)
C 
      DATA IFEED/12/
C
      IER=0
C
C        INITIALIZE THE FORM FEED CHARACTER.
      CFEED=CHAR(IFEED)
C
C        COMPUTE FIRST ORDER DIFFERENCES AND INITIALIZE IFIRST AND
C        ISECOND.
      IFIRST=IA(1)
      ISECOND=IA(2)
C
      DO 120 K=1,NXY-1
         IB(K)=IA(K+1)-IA(K)
C         
         IF(IB(K).GT.LARGE)THEN
            SECOND=.FALSE.
D           WRITE(KFILDO,119)            
D119        FORMAT(/' 1ST ORDER DIFFERENCE EXCEEDS 2**15.',
D    1              '  SECOND ORDER DIFFERENCES ARE NOT PAKCED.')
           GO TO 900
        ENDIF
C
 120  CONTINUE
C
D     WRITE(KFILDO,121)(IA(K),K=1,NXY)
D121  FORMAT(/' ORIGINAL SCALED VALUES'/(' '20I6))
C
C***D     WRITE(KFILDO,122)(IB(K),K=1,NXY-1)
C***D122  FORMAT(/ 'FIRST ORDER DIFFERENCES'/(' '20I6))
C
C        COMPUTE SECOND ORDER DIFFERENCES
C
      DO 130 K=1,NXY-2
         IB(K)=IB(K+1)-IB(K)
 130  CONTINUE
C
C        COMPUTE THE MINIMUM OF THE FIELD OF SECOND ORDER
C        DIFFERENCES AND SUBTRACT IT FROM OF EACH OF THE
C        VALUES IN THE FIELD.
      IMINA=IB(1)
C
      DO 132 K=2,NXY-2
         IF(IB(K).LT.IMINA)IMINA=IB(K)
 132  CONTINUE
C
      DO 134 K=1,NXY-2
         IB(K)=IB(K)-IMINA
 134  CONTINUE
C
C        COMPUTE AVERAGE RANGE OF NXY ORIGINAL VALUES IN INCREMENTS OF 
C        MINPK.
      SUMR=0
      KOUNT=0
C 
      DO 140 K=1,NXY,MINPK
      JMIN=MALLOW
      JMAX=-MALLOW
      IF(K+MINPK-1.GT.NXY)GO TO 140
C        THE LAST GROUP MAY BE VERY SMALL AND NOT BE REPRESENTATIVE OF
C        THE RANGE.
C
      DO 135 J=K,K+MINPK-1
      IF(IA(J).GT.JMAX)JMAX=IA(J)
      IF(IA(J).LT.JMIN)JMIN=IA(J)
 135  CONTINUE
C
      KOUNT=KOUNT+1
      IRANGE=JMAX-JMIN
      SUMR=SUMR+IRANGE    
 140  CONTINUE
C
      AVGR=99999.
      IF(KOUNT.NE.0)AVGR=SUMR/KOUNT
C
C        COMPUTE AVERAGE RANGE OF NXY-2 2ND ORDER VALUES IN INCREMENTS OF 
C        MINPK.
C
      SUMR=0
      KOUNT=0
C
      DO 150 K=1,NXY-2,MINPK
      JMIN=MALLOW
      JMAX=-MALLOW
      IF(K+MINPK-1.GT.NXY-2)GO TO 150
C        THE LAST GROUP MAY BE VERY SMALL AND NOT BE REPRESENTATIVE OF
C        THE RANGE.
C
      DO 145 J=K,K+MINPK-1
      IF(IB(J).GT.JMAX)JMAX=IB(J)
      IF(IB(J).LT.JMIN)JMIN=IB(J)
 145  CONTINUE
C
      KOUNT=KOUNT+1
      IRANGE=JMAX-JMIN
      SUMR=SUMR+IRANGE    
 150  CONTINUE
C
      AVGR2=99999.
      IF(KOUNT.NE.0)AVGR2=SUMR/KOUNT
C
D     WRITE(KFILDO,155)AVGR,AVGR2
D155  FORMAT(/' AVERAGE RANGE OF ORIGINAL SCALED VALUES =  'F10.2/
D    1        ' AVERAGE RANGE OF SECOND ORDER DIFFERENCES ='F10.2)
C
      IF(AVGR2.GE.AVGR)THEN
C
C           ORIGINAL VALUES WILL BE PACKED.  IA( ) HAS NOT BEEN
C           DISTURBED.
C
         SECOND=.FALSE.
      ELSE
C
C           SECOND ORDER DIFFERENCES WILL BE PACKED.  TRANSFER
C           THEM FROM IB( ) TO IA( ).
C
C***D     WRITE(KFILDO,160)
C***D160  FORMAT(' SECOND ORDER DIFFERENCES WILL BE PACKED')
C
C
         IA(1)=0
         IA(2)=0
C
         DO 200 K=1,NXY-2
            IA(K+2)=IB(K)
 200     CONTINUE
C
C***D    WRITE(KFILDO,201)(IA(K),K=1,200)
C***D201 FORMAT(/' SECOND ORDER DIFFERENCES TO BE PACKED'/(' '20I6))
C
         SECOND=.TRUE.
C
C           SINCE WE ARE DOING SECOND ORDER DIFFERENCES
C           TAKE THE TIME HERE TO DETERMINE THE MINIMUM
C           NUMBER OF BYTES (NOT BITS) NEEDED TO CONTAIN 
C           THE FIRST ORIGINAL VALUE, THE SECOND ORIGINAL
C           VALUE, AND THE MINIMUM OF THE FIELD OF SECOND
C           ORDER DIFFERENCES.
C
         NUMBIT=0
         JMINA=ABS(IMINA)
         JFIRST=ABS(IFIRST)
         JSECOND=ABS(ISECOND)
C
C           FIND THE LARGEST ABSOLUTE VALUE OF THE 
C           THE THREE VARIABLES IMINA, IFIRST, AND
C           ISECOND.
C
         IF(JMINA.GT.JFIRST)THEN
            JLARGEST=JMINA
            IVALUE=IMINA
         ELSE
            JLARGEST=JFIRST
            IVALUE=IFIRST
         ENDIF
C
         IF(JLARGEST.LT.JSECOND)THEN
            JLARGEST=JSECOND
            IVALUE=ISECOND
         ENDIF
C
         IBXX2=2
C
         DO 220 K=1,30
            NUMBIT=K
            IF(JLARGEST.LT.IBXX2)GO TO 225
            IBXX2=IBXX2*2
 220     CONTINUE
C
C           DROP THROUGH HERE MEANS THE NUMBER OF BITS
C           EXCEEDS 30 AND THE VALUE TO PACK EXCEEDS 2**30.
C           2**31 IS TOO LARGE A VALUE TO PACK.  2**30 
C           IS USED AS THE LIMIT THROUGHOUT THE PACKER,
C           ALTHOUGH A VALUE OF 2**31-1 COULD PROBABLY
C           BE ACCOMMODATED.
         IER=920
C           THIS IS A FATAL ERROR.
         GO TO 900
C
 225  NUMBIT=NUMBIT+1
C        ALLOW FOR SIGN BIT.
C
C           FIND THE SMALLEST NUMBER OF OCTETS 
C           THAT WILL CONTAIN THE VALUES.  
         NUMOCTET=(NUMBIT+7)/8
C
      ENDIF
C
 900  RETURN
      END
